// pages/reg/reg.js
Page({
  mixins: [require('../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */
  data: {
    company:["请选择"],
    companyindex:"0",
    department:["请选择"],
    departmentindex:"0",
    city:["请选择"],
    cityindex:"0",
    username:"",
    phone:"",
    code:"",
  },

  bindCompanyChange: function(e) {
    var that=this
    var url = wx.getStorageSync("config") 
    console.log(url)
    this.setData({
      companyindex: e.detail.value
    }) 
    console.log(that.data.company[that.data.companyindex]) 
    wx.request({
      url: url.service.homeUrl+'/member/getdepartment',
      data: {
               company:that.data.company[that.data.companyindex]
            },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        console.log(res.data)
        for(var i=0;i<res.data.length;i++)
        { var newarray = [res.data[i].department,];        
         that.data.department = that.data.department.concat(newarray);
         that.setData({
          department: that.data.department
        });  
      }},
      fail: function (res) {console.log(res) },
      complete: function (res) { console.log(res) },
    })
  },

  bindDepartmentChange: function(e) {
    this.setData({
      departmentindex: e.detail.value
    })
  },

  bindCityChange: function(e) {
    this.setData({
      cityindex: e.detail.value
    })
  },

  getUserName:function(e){
    this.setData({
      username:e.detail.value
    })
   },

  getPhone:function(e){
    this.setData({
      phone:e.detail.value
    })
   },

   getCode:function(e){
    this.setData({
      code:e.detail.value
    })
   },

   postReg:function(){
    var that=this
    var openid =wx.getStorageSync('openid')
    var url = wx.getStorageSync("config") 
    wx.request({
      url: url.service.homeUrl+'/member/wxreg',
      data: { 
        department:that.data.department[that.data.departmentindex],
        company:that.data.company[that.data.companyindex],
        city:that.data.city[that.data.cityindex],
        name:that.data.username,
        phone:that.data.phone, 
        openid:openid,
        code:that.data.code          
            },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        }) 
        if(res.data.msg=="注册成功"){
          wx.navigateTo({url: '../index/index',})
        }
        
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.msg,
        }) 
       },
      complete: function (res) { },
    })
  },

  changeTheme: function() {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
},


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var url = wx.getStorageSync("config") 
  
    wx.request({
      url: url.service.homeUrl+'/member/getcompany',
      data: {
           
            },
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        for(var i=0;i<res.data.length;i++)
        { var newarray = [res.data[i].company,];        
         that.data.company = that.data.company.concat(newarray);
         that.setData({
          company: that.data.company
        });  
      }},
      fail: function (res) { },
      complete: function (res) { },
    })

    wx.request({
      url: url.service.homeUrl+'/member/getcity',
      data: {},
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        for(var i=0;i<res.data.length;i++)
        { var newarray = [res.data[i].city,];        
         that.data.city = that.data.city.concat(newarray);
         that.setData({
          city: that.data.city
        });  
      }},
      fail: function (res) { },
      complete: function (res) { },
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})